@extends('admin.pop_edit')
@section('body')
    <form id="systemUserAddForm" action="{{admin_url($baseRoute.'save')}}" class="layui-form model-form">
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">名称:</label>
            <div class="layui-input-block">
                <input type="text" name="name" placeholder="请输入名称" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">编码:</label>
            <div class="layui-input-block">
                <input type="text" name="code" placeholder="请输入模板编码" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">模板内容:</label>
            <div class="layui-input-block">
                <textarea placeholder="请输入模板内容" onblur="contentBlur(this)" name="content"
                          class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">参数:</label>
            <div class="layui-input-block" id="vueApp">
                <el-table
                        :data="params"
                        style="width: 100%">
                    <el-table-column
                            prop="name"
                            label="参数名"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="value"
                            label="参数值"
                            width="180">
                        <template slot-scope="scope">
                            <input type="hidden" :name="`params[${scope.row.name}]`" :value="scope.row.value"/>
                            <el-select v-model="scope.row.value" placeholder="请选择">
                                <el-option v-for="item in options"
                                           :key="item.value"
                                           :label="item.label"
                                           :value="item.value">
                                </el-option>
                            </el-select>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn" lay-filter="ajaxForm" lay-submit>@lang('common.save')</button>
            <button class="layui-btn layui-btn-primary" type="button"
                    ew-event="closeDialog">@lang('common.cancel')</button>
        </div>
    </form>
@stop

@push('script')
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <!-- 引入组件库 -->
    <script src="https://unpkg.com/vue@2/dist/vue.js"></script>
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <script type="text/javascript">
        layui.use(['form', 'table'], function () {
            //第一个实例
            let names = {}, form = layui.form;
            const vueApp = new Vue({
                el: '#vueApp',
                data: function () {
                    return {
                        params: [],
                        options: {!! json_encode($params) !!}
                    }
                }
            })
            window.contentBlur = function (that) {
                let result = that.value.matchAll(/\$\{(.*?)\}/g);
                names = {};
                for (match of result) {
                    names[match[1]] = true;
                }
                for (let i = vueApp.params.length - 1; i >= 0; i--) {
                    let item = vueApp.params[i];
                    if (names[item.name]) {
                        delete names[item.name];
                    } else {
                        vueApp.params.splice(i, 1);
                    }
                }
                for (const name in names) {
                    vueApp.params.push({
                        name,
                        value: ''
                    });
                }
            }
        });
    </script>
@endpush
